<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"> 关键字 </label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" id="search_title" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-primary" id="data-search-btn"><i
                                    class="layui-icon"></i> 搜 索
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary" lay-filter="data-search-btn"><i
                                    class="layui-icon layui-icon-refresh"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!--添加和修改的弹出层开始-->
        <div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
            <form method="post" class="layui-form layui-form-pane" lay-filter="dataFrm" id="dataFrm">
                <div class="layui-form-item" id="div_pid">
                    <label class="layui-form-label">父级部门</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="pid" id="p_id"/>
                        <input type="text" name="pid_name" id="p_name" readonly="readonly" autocomplete="off"
                               placeholder="请选择父级部门" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <a class="layui-btn layui-btn-warm" id="doSelectDept"><label
                                class="layui-icon layui-icon-male"></label>选择</a>
                    </div>

                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门名称</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id"/>
                        <input type="text" name="title" lay-verify="required" autocomplete="off" placeholder="请输入部门名称"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="address" autocomplete="off" placeholder="请输入部门地址" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门备注</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入部门备注" name="remark" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">创建时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="createtime" id="createTime" lay-verify="required"
                                   readonly="readonly" autocomplete="off" placeholder="请选择创建时间" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">排序码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="ordernum" id="ordernum" lay-verify="required" autocomplete="off"
                                   placeholder="请输入排序码" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit="" lay-filter="doSubmit"><label
                                class="layui-icon layui-icon-release"></label>提交</a>
                        <a class="layui-btn layui-btn-warm btn_reset" onclick="javascript:$('#dataFrm')[0].reset()"
                           lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                    </div>
                </div>
            </form>
        </div>
        <!--添加和修改的弹出层结束-->

        <!--表头工具条-->
        <button style="margin-top: 10px;" class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn btn_add"
                id="addDept">
            <i class="layui-icon layui-icon-addition"></i> 添 加
        </button>

        <table class="layui-hide" id="deptTable" lay-filter="deptTable"></table>

        <script type="text/html" id="spreadTpl">
            <input type="checkbox" name="spread" value="{{d.id}}" lay-skin="switch" lay-text="展开|不展开"
                   lay-filter="spreadSwitch"
                   {{ d.spread== 1 ? 'checked' : '' }}>
        </script>

        <script type="text/html" id="availableTpl">
            <input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用"
                   lay-filter="availableSwitch"
                   {{ d.available== 1 ? 'checked' : '' }}>
        </script>

        <!--表格右侧每一行数据的删除按钮-->
        <div style="display: none" id="deptTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete btn_update" lay-event="update"><i class="layui-icon layui-icon-edit"></i> 修 改 </a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete btn_delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i> 删 除 </a>
        </div>
    </div>
</div>

<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui-v2.5.5/layui.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../lib/common/jquery.cookie.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../lib/common/common.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../lib/common/selectLayer.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate', 'layer', 'treeTable'], function () {
        let form = layui.form;
        let table = layui.table;
        let laydate = layui.laydate;
        let layuimini = layui.layuimini;
        let layer = layui.layer;
        let treeTable = layui.treeTable;

        laydate.render({
            elem: '#createTime',
            type: 'datetime'
        });

        // 渲染表格
        let insTb = treeTable.render({
            tree: {
                iconIndex: 1,  // 折叠图标显示在第几列
                idName: 'id',  // 自定义id字段的名称
                pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
                isPidData: true  // 是否是pid形式数据
            },
            elem: '#deptTable',
            height: 'full-250', //高度最大化减去差值
            cols: [
                {type: "numbers"},
                {field: 'title', title: '部门名称', align: "center", width: 200},
                {field: 'remark', title: '部门备注', align: "center"},
                {field: 'address', title: '部门地址', align: "center"},
                {field: 'createtime', title: '发布时间', align: "center"},
                {field: 'ordernum', title: '排序码', align: "center"},
                {field: 'spread', title: '是否展开', align: "center", templet: '#spreadTpl'},
                {field: 'available', title: '是否可用', align: "center", templet: '#availableTpl'},
                {title: '操作', templet: '#deptTableBar', fixed: "right", align: "center"}
            ],
            reqData: function (data, callback) {
                // 在这里写ajax请求，通过callback方法回调数据
                $.get(api + 'dept/loadAllDept', function (res) {
                    callback(res.data);  // 参数是数组类型
                });
            }
        });

        //监听展开操作
        form.on('switch(spreadSwitch)', function (obj) {
            $.post(api + "dept/updateDept", {id: this.value, spread: obj.elem.checked ? 1 : 0}, res => {
                layer.msg(res.msg);
                insTb.reload();
            });
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);

        });

        //监听是否可用操作
        form.on('switch(availableSwitch)', function (obj) {
            $.post(api + "dept/updateDept", {id: this.value, available: obj.elem.checked ? 1 : 0}, res => {
                layer.msg(res.msg);
                insTb.reload();
            });
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
        });

        // 监听搜索操作
        $("#data-search-btn").on("click", function () {
            let title = $("#search_title").val();
            //执行搜索重载
            if (title) {
                insTb.filterData(title);
            } else {
                insTb.reload();
            }
            return false;
        });

        //监听事件
        table.on('toolbar(deptTable)', function (obj) {
            switch (obj.event) {
                case 'batchDelete':
                    var checkStatus = table.checkStatus('deptTable'); //idTest 即为基础参数 id 对应的值
                    var data = checkStatus.data;
                    var length = checkStatus.data.length;

                    if (length == 0) {
                        layer.msg("请选中要删除的行数据");
                    } else {
                        layer.confirm('真的删除选择中这些数据么?', function (index) {
                            var params = "";
                            $.each(data, function (index, item) {
                                if (index == 0) {
                                    params += "ids=" + item.id;
                                } else {
                                    params += "&ids=" + item.id;
                                }
                            });
                            $.post(api + "dept/batchDeleteLoginInfo", params, function (res) {
                                layer.msg(res.msg);
                                tableIns.reload();
                                layer.close(index);
                            })
                        });
                    }
                    break;
            }
        });

        $("#addDept").click(function () {
            openAdddept();
        });

        //监听部门选择的按钮事件打开部门选择的弹出层
        $("#doSelectDept").click(function () {
            var p_id = $("#p_id").val();
            openDeptSelecter(p_id, function (id, name) {
                // alert(id+"---"+name);
                $("#p_id").val(id);
                $("#p_name").val(name);
            });
        });

        var mainIndex;
        var url;  //动态的url
        function openAdddept() {
            mainIndex = layer.open({
                type: 1,
                title: '添加部门',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function (index) {
                    url = api + "dept/addDept";
                    //重置表单
                    $("#dataFrm")[0].reset();
                    //加载最大的排序码
                    $.get(api + "dept/queryDeptMaxOrderNum", function (res) {
                        $("#ordernum").val(res.data);
                    });
                }
            });
        }


        //监听行数据的事件
        treeTable.on('tool(deptTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                $.get(api + "dept/getDeptChildrenCountById", {id: data.id}, function (res1) {
                    if (res1.data > 0) {
                        layer.msg("当前部门有子部门，请选择删除子部门");
                    } else {
                        layer.confirm('真的删除【' + data.title + '】这个部门吗?', function (index) {
                            $.post(api + "dept/deleteDept", {id: data.id}, function (res) {
                                layer.msg(res.msg);
                                obj.del();
                                layer.close(index);
                            })
                        });
                    }
                })
            } else if (obj.event === 'update') {
                openUpdatedept(data);
            }
        });

        //提交数据
        form.on("submit(doSubmit)", function (data) {
            var params = $("#dataFrm").serialize();
            $.post(url, params, function (res) {
                layer.msg(res.msg);
                if (res.code == 200) {
                    insTb.reload();
                }
                layer.close(mainIndex);
            })
            return false;
        })

        function openUpdatedept(data) {
            mainIndex = layer.open({
                type: 1,
                title: '修改[' + data.title + ']部门',
                content: $("#addOrUpdateDiv"),
                area: ['800px', '600px'],
                success: function (index) {
                    url = api + "dept/updateDept";
                    if (data.pid == 0) {//说明是根部门
                        $("#div_pid").hide();
                    } else {
                        $("#div_pid").show();
                    }
                    //装载数据表格
                    form.val("dataFrm", data);
                    $.get(api + "dept/getDeptById", {id: data.pid}, function (res) {
                        $("#p_name").val(res.data.title);
                    })
                }
            });
        }

    });
</script>

</body>
</html>
